package com.zackeus.sipservice;

import com.elvishew.xlog.XLog;

import org.pjsip.pjsua2.Endpoint;
import org.pjsip.pjsua2.OnIpChangeProgressParam;
import org.pjsip.pjsua2.pj_constants_;
import org.pjsip.pjsua2.pjsua_ip_change_op;

/**
 * SIP 端点
 */
public class SipEndpoint extends Endpoint {

    private static final String TAG = SipEndpoint.class.getSimpleName();

    private final SipService service;

    public SipEndpoint(SipService service) {
        super();
        this.service = service;
    }

    /**
     * IP 变更监听
     * @param prm
     */
    @Override
    public void onIpChangeProgress(OnIpChangeProgressParam prm) {
        super.onIpChangeProgress(prm);

        if (prm.getStatus() != pj_constants_.PJ_SUCCESS) {
            XLog.tag(TAG).w("the CallReconnectionState failed, will hangup all calls.");
            hangupAllCalls();
            this.service.getEventEmitter().callReconnectionState(prm.getCallId(), Boolean.FALSE);
            return;
        }

        if (prm.getOp() == pjsua_ip_change_op.PJSUA_IP_CHANGE_OP_COMPLETED) {
            this.service.getEventEmitter().callReconnectionState(prm.getCallId(), Boolean.TRUE);
        }
    }
}
